package com.gx.smart.building.ui.common.popwindow;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.InspectionRegionPopwindowLayoutBinding;
import com.gx.smart.building.ui.inspection.adapter.RegionAdapter;
import com.huishi.gx.ib.grpc.lib.sysuser.sysregionapi.SysRegionDto;

import java.util.ArrayDeque;
import java.util.List;

import razerdp.basepopup.BasePopupWindow;

/**
 * @author: xiaosy
 * 时间: 2022/6/15
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class RegionPopWindow extends BasePopupWindow implements BaseAdapter.OnItemClickListener<SysRegionDto> {
    private InspectionRegionPopwindowLayoutBinding mBinding;
    private RegionAdapter adapter;
    private int level = 1;
    private List<SysRegionDto> source;
    private OnItemSelectedListener mOnItemSelectedListener;
    private ArrayDeque<String> areaName = new ArrayDeque();

    public RegionPopWindow(Context context) {
        super(context);
        setContentView(R.layout.inspection_region_popwindow_layout);
        setViewClickListener(this::click, mBinding.back, mBinding.cancel, mBinding.confirm);
    }

    @Override
    public void onViewCreated(View contentView) {
        mBinding = InspectionRegionPopwindowLayoutBinding.bind(contentView);
        adapter = new RegionAdapter(getContext());
        adapter.setItemClickListener(this);
        mBinding.recyclerView.setAdapter(adapter);
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        @SuppressLint("UseCompatLoadingForDrawables")
        Drawable drawable = getContext().getResources().getDrawable(R.drawable.shape_list_item_divider_line);
        divider.setDrawable(drawable);
        mBinding.recyclerView.addItemDecoration(divider);
    }

    public RegionPopWindow setData(List<SysRegionDto> list) {
        source = list;
        adapter.refresh(list);
        return this;
    }

    public RegionPopWindow setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.mOnItemSelectedListener = listener;
        return this;
    }

    @SuppressLint("NonConstantResourceId")
    void click(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                dismiss();
                break;
            case R.id.confirm:
                selected();
                break;
            case R.id.back:
                back();
                break;
        }
    }

    private void selected() {
        if (adapter.selectedPosition == -1) {
            ToastUtils.showLong("请选择区域");
            return;
        }
        if (mOnItemSelectedListener != null) {
            SysRegionDto item = adapter.getDataList().get(adapter.selectedPosition);
            if (item.getLevel() > 1) {
                areaName.push("/" + item.getName());
            } else {
                areaName.push(item.getName());
            }
            StringBuilder temp = new StringBuilder();
            while (!areaName.isEmpty()) {
                temp.append(areaName.pollLast());
            }
            mOnItemSelectedListener.onSelected(item.getId(), temp.toString());
        }
        dismiss();
    }

    private void back() {
        level--;
        adapter.selectedPosition = -1;
        areaName.pop();
        List<SysRegionDto> current = source;
        do {
            if (current.get(0).getLevel() == level) {
                adapter.refresh(current);
                break;
            } else {
                current = current.get(0).getChildrenList();
            }
        } while (!current.isEmpty());
        if (level == 1) {
            mBinding.back.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(SysRegionDto item, int position) {
        if (item.getChildrenList() == null || item.getChildrenList().isEmpty()) {
            return;
        }
        adapter.selectedPosition = -1;
        adapter.refresh(item.getChildrenList());
        level = item.getChildren(0).getLevel();
        mBinding.back.setVisibility(View.VISIBLE);

        if (item.getLevel() > 1) {
            areaName.push("/" + item.getName());
        } else {
            areaName.push(item.getName());
        }

    }

    @Override
    public void dismiss() {
        super.dismiss();
        adapter.selectedPosition = -1;
    }

    public interface OnItemSelectedListener {
        void onSelected(String areaId, String name);
    }
}
